package com.tuling.bzg.mybatis.spring;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

import java.io.Reader;
import static java.lang.reflect.Proxy.newProxyInstance;

/**
 * 创建SqlSessionFactory
 * */
@Component
public class BzgSqlSessionFactoryBean implements FactoryBean<SqlSessionFactory> {

    @Override
    public SqlSessionFactory getObject() throws Exception {
        String resource = "mybatis-config.xml";
        //将XML配置文件构建为Configuration配置类
        Reader reader = Resources.getResourceAsReader(resource);
        newProxyInstance(null,null,null);
        // 通过加载配置文件流构建一个SqlSessionFactory   解析xml文件  1
        return new SqlSessionFactoryBuilder().build(reader);
    }

    @Override
    public Class<?> getObjectType() {
        return SqlSessionFactory.class;
    }
}
